package cn.ulc.socket.weapp.service.impl;

import cn.ulc.common.constant.PubDataConstant;
import cn.ulc.common.exception.PlatformException;
import cn.ulc.common.model.param.PaginationParam;
import cn.ulc.common.model.param.SortParam;
import cn.ulc.socket.weapp.common.BaseService;
import cn.ulc.socket.weapp.model.dto.StaffOrderDTO;
import cn.ulc.socket.weapp.model.dto.StaffOrderListDTO;
import cn.ulc.socket.weapp.service.StaffOrderServ;
import cn.ulc.store.dao.PubDataDao;
import cn.ulc.store.dao.SetInfoDao;
import cn.ulc.store.dao.order.OrderDao;
import cn.ulc.store.dao.order.OrderItemDao;
import cn.ulc.store.dao.staff.StaffDao;
import cn.ulc.store.dao.staff.StaffDeductBillDao;
import cn.ulc.store.dao.staff.StaffDeductDao;
import cn.ulc.store.dao.staff.StaffOrderDao;
import cn.ulc.store.entity.Company;
import cn.ulc.store.entity.SetInfo;
import cn.ulc.store.entity.order.Order;
import cn.ulc.store.entity.order.OrderItem;
import cn.ulc.store.entity.staff.Staff;
import cn.ulc.store.entity.staff.StaffDeduct;
import cn.ulc.store.entity.staff.StaffDeductBill;
import cn.ulc.store.entity.staff.StaffOrder;
import cn.ulc.store.entity.user.WeixinUser;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
public class StaffOrderServImpl extends BaseService implements StaffOrderServ {

    StaffOrderDao staffOrderDao;
    StaffDao staffDao;
    OrderItemDao itemDao;
    StaffDeductDao staffDeductDao;
    SetInfoDao setInfoDao;
    PubDataDao pubDataDao;
    StaffDeductBillDao billDao;
    OrderDao orderDao;
    @Override
    public StaffOrderListDTO findListPage(Boolean status, PaginationParam param) {
        WeixinUser loginUser = getCurrentUser();
        Company company = getCurrentCompany();
        //角色
        Staff staff = staffDao.findByWxUserId(company.getId(),loginUser.getId());
        if(staff==null) throw new PlatformException("你不是骑手!");
        Map<String,Object> condition = new HashMap<>();
        condition.put("staffId",staff.getId());
        condition.put("companyId",company.getId());
        condition.put("status",status);
        SortParam sortParam = new SortParam("createAt",SortParam.DESC);
        PageQuery pageQuery = initPageQuery(param,sortParam);
        pageQuery.setParas(condition);
        staffOrderDao.findListPage(pageQuery);
        return new StaffOrderListDTO(pageQuery.getList(),pageQuery.getTotalRow());
    }

    @Override
    public StaffOrderDTO findDetail(Long id) {
        StaffOrder staffOrder = staffOrderDao.findById(id);
        if(staffOrder==null)throw new PlatformException("订单查询失败!");
        List<OrderItem> itemList = itemDao.findListByOrderId(staffOrder.getOrderId());
        return new StaffOrderDTO(staffOrder,itemList);
    }

    @Override
    @Transactional
    public Boolean verifyStaffOrder(Long id) {
        WeixinUser loginUser = getCurrentUser();
        Company company = getCurrentCompany();
        Staff staff = staffDao.findByWxUserId(company.getId(),loginUser.getId());
        SetInfo setInfo = setInfoDao.findByCondition(company.getId());
        if(setInfo==null) throw new PlatformException("获取订单提成金额失败!");
        BigDecimal amount = BigDecimal.ZERO;
        //修改骑手订单状态
        StaffOrder staffOrder = staffOrderDao.findById(id);
        if(staffOrder==null)throw new PlatformException("获取订单信息失败!");
        staffOrder.setStatus(true);
        staffOrder.setVerifyDate(new Date());
        staffOrderDao.save(staffOrder);
        Order order = staffOrder.getOrder();
        if(order==null) throw new PlatformException("获取订单信息失败!");
        order.setReceiveTime(new Date());
        order.setStatus(PubDataConstant.ORDER_SENT);
        orderDao.save(order);
        List<OrderItem> itemList = itemDao.findListByOrderId(order.getId());
        if(itemList.size()>0){
            for(OrderItem item:itemList){
                if("DEPOSIT".equals(item.getMode())){
                    amount = amount.add(item.getNum().multiply(setInfo.getDeductWater()));
                }
            }
        }
        if(amount.compareTo(BigDecimal.ZERO)==0) amount = setInfo.getDeduct();
        //更新骑手提成
        StaffDeduct staffDeduct = staffDeductDao.findByStaffId(staff.getId());
        if(staffDeduct == null) {
            staffDeduct = new StaffDeduct();
            setBaseInfo(staffDeduct);
            staffDeduct.setDeduct(amount);
            staffDeduct.setBalance(amount);
            staffDeduct.setStaffId(staff.getId());
            staffDeduct.setCompanyId(company.getId());

        }else{
            setBaseInfo(staffDeduct);
            staffDeduct.setDeduct(amount.add(staffDeduct.getDeduct()));
            staffDeduct.setBalance(amount.add(staffDeduct.getBalance()));
        }
        staffDeductDao.save(staffDeduct);
        //创建提成账单
        String code = pubDataDao.findCodeByTypeAndName(PubDataConstant.DATA_TYPE_BILL,"骑手佣金");
        StaffDeductBill bill = new StaffDeductBill();
        setBaseInfo(bill);
        bill.setStaffId(staff.getId());
        bill.setOrderNo(staffOrder.getOrderNo());
        bill.setBillType(code);
        bill.setEiType("+");
        bill.setAmount(amount);
        bill.setDealDate(new Date());
        bill.setCompanyId(company.getId());
        bill.setRemark("备注");
        billDao.save(bill);
        return true;
    }
}
